package com.share.sharedevice.service.impl;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.common.core.utils.bean.BeanUtils;
import com.share.sharedevice.entity.Region;
import com.share.sharedevice.mapper.RegionMapper;
import com.share.sharedevice.service.IRegionService;
import com.share.sharedevice.vo.RegionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements IRegionService {


    @Autowired
    private RegionMapper regionMapper;

    /**
     * 获取区域树列表
     *
     * @param
     * @return
     */
    @Override
    public List<Region> treeSelect(String code){
        //获取节点信息
        Region region = regionMapper.selectByRegionId(code);

        //查询当前节点下的所有子节点
        List<Region> regions = regionMapper.selectTreeSelect(code);

        if (!CollectionUtils.isEmpty(regions)) {
            regions.forEach(item -> {
                long count = regionMapper.selectCount(new LambdaQueryWrapper<Region>().eq(Region::getParentCode, item.getCode()));
                //判断是否有子节点
                if (count > 0) {
                    item.setHasChildren(true);
                } else {
                    item.setHasChildren(false);
                }
            });
        }

        if (region != null){
            //判断是否为根节点
            if(region.getParentCode().equals("0")){  // 修改判断条件
                //若是根节点，则构建子节点树
                if (!CollectionUtils.isEmpty(regions)) {
                    buildTree(regions);  // 构建子树
                }
                return regions;
            } else {
                //若不是根节点，也构建子树结构
                if (!CollectionUtils.isEmpty(regions)) {
                    buildTree(regions);
                }
                return regions;
            }

        }else {
            throw new RuntimeException("未找到该节点");
        }

    }

    // 新增辅助方法用于构建树形结构
    private void buildTree(List<Region> regions) {
        regions.forEach(item -> {
            String code = item.getCode();
            //获取子节点
            List<Region> childRegions = regionMapper.selectTreeSelect(code);
            //判断子节点是否有子节点
            if (!CollectionUtils.isEmpty(childRegions)) {
                //如果有子节点，则设置子节点并递归构建
                item.setChildren(childRegions);
                buildTree(childRegions);  // 递归构建子树
            } else {
                item.setHasChildren(false);
            }
        });
    }

    /**
     * 获取区域树列表
     *
     * @param
     * @return
     */
    @Override
    public RegionVo getByRegionId(String code) {

        Region region = regionMapper.getByRegionId(code);

        RegionVo regionVo = new RegionVo();

        BeanUtils.copyBeanProp(regionVo, region);

        return regionVo;
    }
}
